﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MuaChung.Models
{
    public class SendQuestionModel
    {
        public string emailSender { get; set; }
        public string questionContent { get; set; }
    }
    public class AnswerModel
    {
        public int id { get; set; }
        public string email { get; set; }
        public string noiDungCauHoi { get; set; }
        public string noiDungTraLoi { get; set; }
    }
    public class HelpModel
    {
        private static MuaChungEntities db = new MuaChungEntities();
        public static bool SendQuestion(SendQuestionModel model)
        {
            TROGIUP tmp = new TROGIUP();
            tmp.Email = model.emailSender;
            tmp.NoiDung = model.questionContent;
            tmp.NgayHoi = DateTime.Now;
            tmp.TinhTrang = 0;

            db.AddToTROGIUPs(tmp);
            db.SaveChanges();
            return true;
        }
        public static List<TROGIUP> GetAll(int currentPage = 1, int pageSize = 10)
        {
            int startindex = pageSize * (currentPage - 1);
            int endindex = pageSize * currentPage;
            try
            {
                var query = (from n in db.TROGIUPs
                             orderby n.ID descending
                             select n).Skip(startindex);
                if (query.Count() > 0)
                {
                    return query.Take(pageSize).ToList();
                }
                else
                {
                    return null;
                }
            }
            catch { return null; }
        }
        public static int GetNumberQuestion()
        {
            try
            {
                var query = from n in db.TROGIUPs
                            select n;
                return query.Count();
            }
            catch
            {
                return 0;
            }
        }
        public static bool UpdateQuestion(TROGIUP tg)
        {
            try
            {
                db.TROGIUPs.ApplyCurrentValues(tg);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static TROGIUP FindByID(int id)
        {
            try
            {
                var query = from n in db.TROGIUPs
                            where n.ID == id
                            select n;
                return query.First();
            }
            catch
            {
                return null;
            }
        }
        public static List<TROGIUP> GetAllNoAnswer(int currentPage = 1, int pageSize = 10)
        {
            int startindex = pageSize * (currentPage - 1);
            int endindex = pageSize * currentPage;
            try
            {
                var query = (from n in db.TROGIUPs
                             orderby n.ID descending
                             where n.TinhTrang == 0
                             select n).Skip(startindex);
                if (query.Count() > 0)
                {
                    return query.Take(pageSize).ToList();
                }
                else
                {
                    return null;
                }
            }
            catch { return null; }
        }
    }
}